package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.TravelGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/travelgroup")
public class TravelGroupController {
    @Reference  //@Reference用于dubbo消费者服务指明引用哪一个提供者的接口服务
            TravelGroupService travelGroupService;

    //添加
    @RequestMapping("/add.do")
    public Result add(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds) {
        try {
            travelGroupService.add(travelGroup, travelItemIds);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    //分页
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = travelGroupService.findPage(queryPageBean);
        return pageResult;
    }

    //回显getGroupById
    @RequestMapping("/getGroupById.do")
    public Result getGroupById(Integer id) {
        TravelGroup travelGroup = travelGroupService.getGroupById(id);
        return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS, travelGroup);
    }

    //查询跟团游对应的自由行的id
    @RequestMapping("/getItemById.do")//getItemById
    public Result getItemById(Integer id) {
        List<Integer> ids = travelGroupService.getItemById(id);
        return new Result(true, "查询跟团游和自由行关联成功", ids);
    }

    //编辑
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds) {
        try {
            travelGroupService.edit(travelGroup, travelItemIds);
            return new Result(true, MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

    //删除
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try {
            travelGroupService.delete(id);
            return new Result(true, MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    //查询所有的跟团游信息
    @RequestMapping("/findAll.do")
    public Result findAll(){
      List<TravelGroup> travelGroupList = travelGroupService.findAll();
      return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,travelGroupList);
    }

}
